<template>
  <div class="login-container">
    <a-card class="login-card" title="博客后台管理系统">
      <a-tabs v-model:activeKey="activeKey">
        <a-tab-pane key="login" tab="登录">
          <a-form
            :model="loginForm"
            @finish="handleLogin"
          >
            <a-form-item
              name="username"
              :rules="[{ required: true, message: '请输入用户名' }]"
            >
              <a-input v-model:value="loginForm.username" placeholder="用户名">
                <template #prefix>
                  <user-outlined />
                </template>
              </a-input>
            </a-form-item>
            
            <a-form-item
              name="password"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password v-model:value="loginForm.password" placeholder="密码">
                <template #prefix>
                  <lock-outlined />
                </template>
              </a-input-password>
            </a-form-item>
            
            <a-form-item>
              <a-button type="primary" html-type="submit" class="login-button" :loading="loading">
                登录
              </a-button>
            </a-form-item>
          </a-form>
        </a-tab-pane>
        
        <a-tab-pane key="register" tab="注册">
          <a-form
            :model="registerForm"
            @finish="handleRegister"
          >
            <a-form-item
              name="username"
              :rules="[{ required: true, message: '请输入用户名' }]"
            >
              <a-input v-model:value="registerForm.username" placeholder="用户名">
                <template #prefix>
                  <user-outlined />
                </template>
              </a-input>
            </a-form-item>
            
            <a-form-item
              name="password"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password v-model:value="registerForm.password" placeholder="密码">
                <template #prefix>
                  <lock-outlined />
                </template>
              </a-input-password>
            </a-form-item>
            
            <a-form-item
              name="nickname"
              :rules="[{ required: true, message: '请输入昵称' }]"
            >
              <a-input v-model:value="registerForm.nickname" placeholder="昵称">
                <template #prefix>
                  <user-outlined />
                </template>
              </a-input>
            </a-form-item>
            
            <a-form-item
              name="email"
              :rules="[
                { required: true, message: '请输入邮箱' },
                { type: 'email', message: '请输入有效的邮箱地址' }
              ]"
            >
              <a-input v-model:value="registerForm.email" placeholder="邮箱">
                <template #prefix>
                  <mail-outlined />
                </template>
              </a-input>
            </a-form-item>
            
            <a-form-item>
              <a-button type="primary" html-type="submit" class="login-button" :loading="loading">
                注册
              </a-button>
            </a-form-item>
          </a-form>
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user';
import { register } from '@/api/auth';

const userStore = useUserStore();
const router = useRouter();
const loading = ref(false);
const activeKey = ref('login');

const loginForm = ref({
  username: '',
  password: ''
});

const registerForm = ref({
  username: '',
  password: '',
  nickname: '',
  email: ''
});

const handleLogin = async () => {
  loading.value = true;
  try {
    await userStore.login(loginForm.value);
    message.success('登录成功');
    router.push('/');
  } catch (error) {
    console.error('登录失败:', error);
  } finally {
    loading.value = false;
  }
};

const handleRegister = async () => {
  loading.value = true;
  try {
    await register(registerForm.value);
    message.success('注册成功，请登录');
    activeKey.value = 'login';
    loginForm.value.username = registerForm.value.username;
    loginForm.value.password = '';
  } catch (error) {
    console.error('注册失败:', error);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 360px;
}

.login-button {
  width: 100%;
}
</style> 